package com.yiren.dbaa.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

/**
 * Describe 获取SIM卡信息和手机号码
 */
public class PhoneInfoUtil {

    private static String TAG = "PhoneInfoUtil";

    private TelephonyManager telephonyManager;
    //移动运营商编号
    private String NetworkOperator;
    private Context context;

    public PhoneInfoUtil(Context context) {
        this.context = context;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }

    /**
     * 获取sim卡iccid
     *
     * @return
     */
    public String getIccid() {
        String iccid = "N/A";
//       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R){
//           iccid = telephonyManager.getSimSerialNumber();
//       }

        return iccid;
    }

    public String getSimState() {
        String simState = "";
        switch (telephonyManager.getSimState()) {
            case TelephonyManager.SIM_STATE_ABSENT:
                simState = "无卡";
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                simState = "未知状态";
                break;
            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                simState = "需要NetworkPIN解锁";
                break;
            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                simState = "需要PIN解锁";
                break;
            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                simState = "需要PUK解锁";
                break;
            case TelephonyManager.SIM_STATE_READY:
                simState = "已激活";
                break;
        }
        return simState;
    }

    /**
     * 获取电话号码
     *
     * @return
     */
    public String getNativePhoneNumber() {
        String nativePhoneNumber = "N/A";
        nativePhoneNumber = telephonyManager.getLine1Number();
        return nativePhoneNumber;
    }

    /**
     * 获取手机服务商信息
     *
     * @return
     */
    public String getProvidersName() {
        String providersName = "";
        NetworkOperator = telephonyManager.getNetworkOperator();
        //IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
        if (NetworkOperator.equals("46000") || NetworkOperator.equals("46002")) {
            providersName = "中国移动";
        } else if (NetworkOperator.equals("46001")) {
            providersName = "中国联通";
        } else if (NetworkOperator.equals("46003")) {
            providersName = "中国电信";
        }
        return providersName;

    }

    public String getPhoneInfo() {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        StringBuffer sb = new StringBuffer();
        sb.append("\nLine1Number = " + tm.getLine1Number());
        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());//移动运营商编号
        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());//移动运营商名称
        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
        sb.append("\nSimOperator = " + tm.getSimOperator());
        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
        sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
        sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
        return sb.toString();
    }

    @SuppressLint("MissingPermission")
    public void getPhoneBaseInfo() {
        CellLocation cel = telephonyManager.getCellLocation();
        if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {//如果是电信卡的话
            CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cel;
            int cid = cdmaCellLocation.getBaseStationId();
            int lac = cdmaCellLocation.getNetworkId();
        } else {//如果是移动和联通的话  移动联通一致
            GsmCellLocation gsmCellLocation = (GsmCellLocation) cel;
            int cid = gsmCellLocation.getCid();
            int lac = gsmCellLocation.getLac();
        }
    }


}
